import nibabel as nib
from nibabel.viewers import OrthoSlicer3D
import mycode.Tool as Tool
import matplotlib.pyplot as plt


class DataPreProcess:
    def __init__(self, path):
        self.INPUT_FOLDER = path
        # 建立一个列表，用于存储所有需要的nii文件的地址
        self.lstFilesNII = Tool.Load(self.INPUT_FOLDER, '.nii').load_scan()

    def read(self):
        example = self.lstFilesNII[0]
        img = nib.load(example)
        print("该文件的格式: ", img)
        print("该文件的头文件: ", img.header)
        print("该文件头文件中的db_name属性的值: ", img.header['db_name'])
        print("该文件的数据: ", img.dataobj)
        width, height, seq = img.dataobj.shape
        print("该文件的数据维度", (width, height, seq))
        # 3D的展示，可以设置显示的区域坐标
        o = OrthoSlicer3D(img.dataobj)
        o.set_position(255,255,12)
        o.show()

        # 2D切片的展示,以横截面为例
        img_arr = img.dataobj[:, :, 5]
        plt.imshow(img_arr, cmap='hot')
        plt.show()

    def run(self):
        self.read()